package com.zw.yzk.kabu.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.constant.ActivityConstant;
import com.zw.yzk.kabu.model.TicketOrderModel;
import com.zw.yzk.kabu.utils.ResUtils;
import com.zw.yzk.kabu.view.activity.TicketOrderDetailsActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by zhanwei on 2017/2/9.
 */

public class TickerOrderListAdapter extends RecyclerView.Adapter<TickerOrderListAdapter.Holder> {

    private List<TicketOrderModel> dataList;
    private Context context;

    public TickerOrderListAdapter(List<TicketOrderModel> list) {
        dataList = new ArrayList<>();
        if (list != null) {
            dataList.addAll(list);
        }
    }

    public void refresh(List<TicketOrderModel> list) {
        dataList.clear();
        if (list != null) {
            dataList.addAll(list);
        }
        notifyDataSetChanged();
    }

    public void add(List<TicketOrderModel> list) {
        if (list != null) {
            int position = dataList.size();
            dataList.addAll(list);
            notifyItemRangeInserted(position, list.size());
        }
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (context == null) {
            context = parent.getContext();
        }
        View view = LayoutInflater.from(context).inflate(R.layout.recycler_item_ticket_order_list, parent, false);
        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(Holder holder, int position) {
        final TicketOrderModel model = dataList.get(position);
        holder.orderId.setText(model.getId());
        holder.orderState.setText("已支付");
        holder.name.setText(model.getTitle());
        holder.price.setText(ResUtils.formatCurrency(context, model.getPrice()));
        holder.count.setText(context.getResources().getString(R.string.goods_count, model.getCount()));
        holder.coast.setText(ResUtils.formatCurrency(context, model.getTotalPrice()));

        Glide.with(context)
                .load(model.getImage())
                .error(R.drawable.im_default)
                .into(holder.image);

        holder.viewOrder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, TicketOrderDetailsActivity.class);
                intent.putExtra(ActivityConstant.EXHIBITION_TICKET_ORDER_ID, model.getId());
                context.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    public static class Holder extends RecyclerView.ViewHolder {
        @Bind(R.id.order_id)
        TextView orderId;
        @Bind(R.id.order_state)
        TextView orderState;
        @Bind(R.id.image)
        ImageView image;
        @Bind(R.id.name)
        TextView name;
        @Bind(R.id.price)
        TextView price;
        @Bind(R.id.count)
        TextView count;
        @Bind(R.id.coast)
        TextView coast;
        @Bind(R.id.time)
        TextView time;
        @Bind(R.id.view_order)
        TextView viewOrder;

        public Holder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
